package com.manager.condition.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.manager.common.result.Result;
import com.manager.condition.service.CustomerRecordService;
import com.manager.condition.service.ConditionCustomerService;
import com.manager.condition.service.TypeService;
import com.manager.condition.service.UserService;

import com.manager.model.Recorder.CustomerRecord;
import com.manager.model.model.Customer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@Api(tags="客户情况管理")
@RequestMapping("/situation")
public class ConditionController {
    @Autowired
    private ConditionCustomerService customerService;
    @Autowired
    private UserService userService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private CustomerRecordService customerRecordService;
    @ApiOperation(value = "分页查询所有客户信息")
    @GetMapping("/get/page/{currentPage}/{rows}")
    public Result getCusPage(@PathVariable long currentPage,
                             @PathVariable long rows
    ) {
        //创建page对象，传递当前页，每页记录数
        Page<Customer> page = new Page<>(currentPage,rows);
        //构建条件，用条件构造器
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.isNotNull("seller_id");//客户的销售id不为空
        wrapper.orderByAsc("createTime");//将查询到的数据按创建时间的由早到晚排列，升序
        IPage<CustomerRecord> page1 = customerRecordService.getCustomerRecordPage(page, wrapper);
        //返回结果
        return Result.ok(page1);
    }
    //2.根据姓名查找客户
    @ApiOperation(value = "根据姓名查找客户")
    @GetMapping("/get/{currentPage}/{rows}/{name}")
    public Result getCusByName(@PathVariable long currentPage,
                               @PathVariable long rows,
                               @PathVariable String name
    ) {
        //创建page对象，传递当前页，每页记录数
        Page<Customer> page = new Page<>(currentPage,rows);
        //构建条件
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
            wrapper.orderByAsc("createTime");
        }
        IPage<CustomerRecord> page1 = customerRecordService.getCustomerRecordPage(page, wrapper);
        //返回结果
        return Result.ok(page1);
    }
    //3.根据进展情况对客户分页查询(flag为1.全部客户;2.已完成客户;3.进行中客户;4.新客户)
    @ApiOperation(value = "点击查看不同状态客户")
    @GetMapping("/get/by-flag/{currentPage}/{rows}/{flag}")
    public Result getCusByFlag(@PathVariable long currentPage,
                               @PathVariable long rows,
                               @PathVariable Integer flag
    ){
        //创建page对象，传递当前页，每页记录数
        Page<Customer> page = new Page<>(currentPage,rows);
        //构建条件，用条件构造器
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("createTime");//将查询到的数据按创建时间的由早到晚排列，升序
        //根据status的值来查看不同客户 status为1.全部客户;2.已完成客户;3.进行中客户;4.新客户
        if(flag==2){//2.查看已完成客户
            wrapper.eq("money_status",0);//0 已付定金
            wrapper.eq("sign_status",0);//0 已签约
        }
        if(flag==3){//3.查看进行中客户
            wrapper.eq("sign_status",1);//1  未签约
            wrapper.eq("enroll_status",0);//0 已报名

        }
        if(flag==4){//3.查看新客户
            wrapper.eq("sign_status",1);//1  未签约
            wrapper.eq("enroll_status",1);//1 未报名
            wrapper.eq("money_status",1);//1 未付定金
        }

        IPage<CustomerRecord> page1 = customerRecordService.getCustomerRecordPage(page, wrapper);
        //返回结果
        return Result.ok(page1);
    }
}
